﻿using KZ.Express.ENT;
using KZ.Express.Resource;
using KZ.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KZ.Express.Task
{
    public class AccountTask : TaskBase
    {
        private new SupplierENT _entity;

        public AccountTask()
            : base()
        {
            base._entity = _entity = new SupplierENT();
            _entitytype = _entity.GetType();
        }

        public override int Insert(EntityBase entity)
        {
            base._dalBase.BeginTransaction();

            SupplierENT supplier = entity as SupplierENT;
            ValidateData(supplier);
            try
            {
                supplier.PartnerID = _dalBase.Insert(supplier);

                PartnerCarrierENT partnerCarrier = new PartnerCarrierENT();
                partnerCarrier.PartnerID = supplier.PartnerID;
                partnerCarrier.CarrierID = supplier.CarrierID;

                _dalBase.Insert(partnerCarrier);

                base._dalBase.CommitTransaction();
                return supplier.PartnerID;
            }
            catch (DataAccessException ex)
            {
                base._dalBase.RollbackTransaction();
                throw new BusinessLogicException(ex.Message);
            }
            catch
            {
                base._dalBase.RollbackTransaction();
                throw new BusinessLogicException(Message.BLM_ERR_INSERT.ToString());
            }
        }

        public override int Update(EntityBase entity)
        {
            base._dalBase.BeginTransaction();

            SupplierENT supplier = entity as SupplierENT;
            ValidateData(supplier);
            try
            {
                supplier.PartnerID = _dalBase.Update(supplier);

                PartnerCarrierENT partnerCarrier = new PartnerCarrierENT();
                partnerCarrier.PartnerID = supplier.PartnerID;
                partnerCarrier.CarrierID = supplier.CarrierID;

                _dalBase.Insert(partnerCarrier);

                base._dalBase.CommitTransaction();
                return supplier.PartnerID;
            }
            catch
            {
                base._dalBase.RollbackTransaction();
                throw new BusinessLogicException(Message.BLM_ERR_INSERT.ToString());
            }
        }
    }
}
